'use strict';
const _ = require('lodash');

class Connector {
    constructor(ctx){
        this.ctx = ctx;
    }

    async getAnswer(filters){
        let data = [], total = 0;
        const conditions = {};

        if(filters?.user_id) {
            conditions.user_id = filters.user_id;
        }

        try {
            total = await this.ctx.model.Answer.find(conditions).count();
            data = await this.ctx.model.Answer.find(conditions).skip((current - 1)*pageSize).limit(pageSize);
        } catch (error) {
            this.ctx.throw(500, '获取回答内容失败');
        }

        return { data, total };
    }

    async addAnswers(answers){
        return await this.ctx.service.answer.addAnswers(answers);
    }

    async delAnswer(ids){
        try {
            await this.ctx.model.Answer.deleteMany({
                id: {
                    $in: ids
                }
            });
        } catch (error) {
            this.ctx.throw(500, '删除回答失败');
        }
        return ids;
    }
}

module.exports = Connector;